package com.yanqu.road.dao.impl.config;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.cricket.config.CricketConfig;
import com.yanqu.road.entity.cricket.config.CricketCourtConfig;
import com.yanqu.road.entity.cricket.config.CricketScenceConfig;
import com.yanqu.road.entity.cricket.config.CricketUpConfig;
import com.yanqu.road.utils.ConfigHelper;
import com.yanqu.road.utils.config.Config;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CricketConfigDaoImpl extends TempDao {

    private static final String getCricketConfigSql = "select * from t_p_cricket";

    public Map<Integer, CricketConfig> getCricketConfig() {
        Map<Integer, CricketConfig> result = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getCricketConfigSql);
            rs = call.executeQuery();
            while (rs.next()) {
                CricketConfig data = new CricketConfig();
                data.setId(rs.getInt("id"));
                data.setQuality(rs.getInt("quality"));
                data.setTalent(rs.getString("talent"));
                data.setBeginTitle(rs.getString("beginTitle"));
                data.setAttackSkills(rs.getString("attackSkills"));
                data.setSkills(rs.getString("skills"));
                data.setOccupation(rs.getInt("occupation"));
                data.setPartonSkill(rs.getInt("partonSkill"));
                data.setRelationPoint(rs.getString("relationPoint"));
                data.setEquipSkills(rs.getString("equipID"));
                data.setGetByEvent(rs.getInt("getByEvent"));
                result.put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    private static final String getCricketUpConfigSql = "select * from t_p_cricket_up";

    public Map<Integer, CricketUpConfig> getCricketUpConfig() {
        Map<Integer, CricketUpConfig> result = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getCricketUpConfigSql);
            rs = call.executeQuery();
            while (rs.next()) {
                CricketUpConfig data = new CricketUpConfig();
                data.setId(rs.getInt("id"));
                data.setCricketQuality(rs.getInt("cricketQuality"));
                data.setTitle(rs.getInt("title"));
                data.setLittleTitle(rs.getInt("littleTitle"));
                data.setSelfConsume(rs.getInt("selfConsume"));
                data.setAddProperty(rs.getString("addProperty"));
                data.setUnlockSkill(rs.getInt("unlockSkill"));
                data.setReward(rs.getString("reward"));
                result.put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    private static final String getCricketScenceConfigSql = "select * from t_p_cricket_scence";

    public Map<Integer, CricketScenceConfig> getCricketScenceConfig() {
        Map<Integer, CricketScenceConfig> result = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getConfigDb());
            call = getDb().getConnection().prepareCall(getCricketScenceConfigSql);
            rs = call.executeQuery();
            while (rs.next()) {
                CricketScenceConfig data = new CricketScenceConfig();
                data.setId(rs.getInt("id"));
                data.setCatchWeight(rs.getString("catchWeight"));
                data.setCricketLibrary(rs.getString("cricketLibrary"));
                data.setWishPool(rs.getString("wishPool"));
                data.setOrder(rs.getInt("order"));
                result.put(data.getId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    private static final String getCricketCourtConfigSql = "select * from t_s_cricket_court_config where channelId = ? ";

    public Map<Integer, CricketCourtConfig> getCricketCourtConfig(int channelId) {
        Map<Integer, CricketCourtConfig> result = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getCricketCourtConfigSql);
            int i = 1;
            call.setInt(i++, channelId);
            rs = call.executeQuery();
            while (rs.next()) {
                CricketCourtConfig data = new CricketCourtConfig();
                data.setCourtId(rs.getInt("courtId"));
                data.setChannelId(rs.getInt("courtId"));
                data.setCourtType(rs.getInt("courtType"));
                data.setCourtName(rs.getString("courtName"));
                data.setFightRangeServer(rs.getInt("fightRangeServer"));
                data.setFightRangeDay(rs.getString("fightRangeDay"));
                data.setNum(rs.getInt("num"));
                data.setGroupLimit(rs.getInt("groupLimit"));
                data.setUpNum(rs.getInt("upNum"));
                data.setKeepNum(rs.getInt("keepNum"));
                data.setReward(rs.getString("reward"));
                data.setRankReward(rs.getString("rankReward"));
                data.setCourtState(rs.getInt("courtState"));
                data.setModLimit(rs.getInt("modLimit"));
                result.put(data.getCourtId(), data);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }



}
